package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.UserMapper;
import org.dey.pojo.Menu;
import org.dey.pojo.PageVo;
import org.dey.pojo.User;
import org.dey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author deyran
 * @classname
 * @see ServiceImpl
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    UserMapper userMapper;

    /*@Override
    public List<User> USER_LIST() {
        return this.list();
    }*/

    @Override
    public List<User> USER_LIST() {
        return userMapper.USERS();
    }

    @Override
    public PageVo<User> queryuser(User user, int pageNum, int pagesize) {


        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(StringUtils.isNotBlank(user.getUsername())){
            queryWrapper.like("username",user.getUsername());
        }

        PageHelper.startPage(pageNum, pagesize);

        List<User> list = this.list(queryWrapper);

        PageInfo<User> pageInfo = new PageInfo<User>(list);


        PageVo<User> pageVo = new PageVo<User>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;
    }

    @Override
    public Result insertuser(User user) {
        Result result = null;

        boolean one = this.save(user);

        if(one){
            result = new Result(true, MessageConstant.ADD_USER_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_USER_FAIL);
        }

        return result;

    }

    @Override
    public Result deluser(int id) {
        Result result =null;
        boolean b = this.removeById(id);
        if(b){
            result = new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_USER_FAIL);
        }
        return result;
    }

    @Override
    public User queryuserbyid(int id) {
        return this.getById(id);
    }

    @Override
    public Result updateuserbyid(User user) {
        Result result =null;
        boolean b = this.updateById(user);
        if(b){
            result = new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.EDIT_USER_FAIL);
        }
        return result;
    }

    @Override
    public List<Menu> queryallmenu(String name, int pid) {
        return null;
    }

    @Override
    public int LOGIN(User user) {


        int userid = 0;

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername()).eq("password",user.getPassword());

        User one = this.getOne(queryWrapper);
      if(one!=null){
          userid = one.getId();
      }
        return userid;
    }

    @Override
    public Result addroleqq(int userid, int roleid) {

        Result result = null;

        //先根据查找有该用户没有该角色
        int queryrole = userMapper.queryrole(userid, roleid);

        if(queryrole!=1){
            int addroleqq = userMapper.addroleqq(userid, roleid);
            if(addroleqq==1){
                result = new Result(true,"添加成功");
            }
        }else{
            result = new Result(true,"已有该角色");
        }

        return result;
    }

}
